package com.simtek.simplayer;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Toast;

import com.simtek.simplayer.controller.FetchUrlAsyncTask;
import com.simtek.simplayer.controller.FetchUrlAsyncTask.FetchUrlCallback;
import com.simtek.simplayer.controller.FetchUrlAsyncTask.FetchUrlCallbackEntity;

public class WebAppInterfaceImp {
	Context							context;
	WebView							webview;
	public static ExecutorService	executorService	= null;

	public WebAppInterfaceImp(Context context, WebView webview) {
		this.context = context;
		this.webview = webview;
	}

	@JavascriptInterface
	public void fetchUrl(String httpUrl, String headers, String callbackId) {
		fetchHttpUrl(httpUrl, "GET", null, headers, callbackId);
	}
	
	@JavascriptInterface
	public void fetchHttpUrl(String httpUrl, String method, String postData, String headers, String callbackId) {
		FetchUrlCallbackEntity item = new FetchUrlCallbackEntity();
		item.httpURL = httpUrl;
		item.callbackId = callbackId;
		item.method = method;
		item.postData = postData;
		item.headers = headers.split("\\|");
		item.listener = new FetchUrlCallback() {
			@Override
			public void receiveJS(String js) {
				webview.loadUrl(js);
			}
		};

		if (android.os.Build.VERSION.SDK_INT >= 11) {
			if (executorService == null) {
				executorService = new ThreadPoolExecutor(1, 1, 5000L,
						TimeUnit.MILLISECONDS, new LinkedBlockingQueue(),
						new ThreadFactory() {
							@Override
							public Thread newThread(Runnable r) {
								Thread thread = new Thread(r);
								thread.setName("Bum JS");
								return thread;
							}
						});
			}
			new FetchUrlAsyncTask().executeOnExecutor(executorService, item);
		} else {
			try {
				new FetchUrlAsyncTask().execute(item);
			} catch (Throwable throwable) {
				throwable.printStackTrace();
			}
		}
	}

	@JavascriptInterface
	public void openHttp(final String httpUrl) {
		webview.post(new Runnable() {
			public void run() {
				Intent i = new Intent(Intent.ACTION_VIEW);
				i.setClass(webview.getContext(), VideoOpenHandlerActivity.class);
				i.setData(Uri.parse(httpUrl));
				context.startActivity(i);
			}
		});
	}

	@JavascriptInterface
	public void playVideoUrl(final String url, final String title, final String uid) {
		Log.d("down", "playVideoUrl " + url + " " + title + uid);
		webview.post(new Runnable() {
			public void run() {
				Intent i = new Intent(Intent.ACTION_VIEW);
				i.setClass(webview.getContext(), VideoOpenHandlerActivity.class);
				i.setData(Uri.parse(url));
				i.putExtra(Intent.EXTRA_TITLE, title);
				i.putExtra(Intent.EXTRA_UID, uid);
				context.startActivity(i);
			}
		});
	}

	@SuppressWarnings("deprecation")
	@SuppressLint("NewApi")
	@JavascriptInterface
	public void copyToClipboard(String text) {
		int sdk = android.os.Build.VERSION.SDK_INT;
		if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
			android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context
					.getSystemService(Context.CLIPBOARD_SERVICE);
			clipboard.setText(text);
		} else {
			ClipboardManager clipboard = (ClipboardManager) context
					.getSystemService(Context.CLIPBOARD_SERVICE);
			ClipData clip = ClipData.newPlainText("Android ID", text);
			clipboard.setPrimaryClip(clip);
		}

		showToast("Copied");
	}

	@JavascriptInterface
	public void showToast(String toast) {
		Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
	}

	@JavascriptInterface
	public String getDeviceId() {
		return Settings.Secure.getString(context.getContentResolver(),
				Settings.Secure.ANDROID_ID);
	}
}